import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';
import 'package:yp/utils/util.dart';
import 'package:fluwx/fluwx.dart' as fluwx;
import 'order_state.dart';

class OrderLogic extends GetxController {
  final OrderState state = OrderState();

  @override
  void onReady() {
    var condition = {
      "use_platform": 2,
      "usr_num_id": SpUtil.getInt('empeNumId'),
      "card_status": 3, //购物卡状态 (1: 有效, 2:失效, 3: 可用)
      'access_token': SpUtil.getString('token'),
    };
    Api.walletInformation(condition).listen((event) {
      print('===========walletInformation==========');
      print(event);
      /*
      * {code: 0, message: 成功, full_message: null, available_cards: [],
      * invalid_cards: null, available_cards: [], valid_card_count: 0, total_card_count: 0,
      * invalid_card_count: null, total_available_amount: 0.0}
      *
      * */
      if (event['code'] == 0) {
        state.GiftcardList = event['available_cards'] ?? [];
        if (state.GiftcardList.length > 0) {
          for (var i = 0; i < state.GiftcardList.length; i++) {
            state.GiftcardList[i]['click'] = false;
          }
        }
      }
      update();
    });
  }

  void goToMoney(id) {
    var params1 = {
      'access_token': SpUtil.getString('token'),
      "out_trade_no": id,
      "plat_type": 7,
      "body": "jifen",
      "pay_way": 0,
      "use_platform": 2,
      'appid': Config.ZJ_APPID,
      // 'ec_shop_num_id': SpUtil.getInt('storeShopNum'),
      "ec_shop_num_id": SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
    };

    gotomoney(state, startCPCNWeixinPay, params1, id);
  }

  /// 拉起中金支付
  void startCPCNWeixinPay(String authCode) async {
    if (authCode.isEmpty) {
      return;
    }

    var result =
        await fluwx.payWithCPCN(appId: Config.WECHAT_APPID, authCode: authCode);

    // var result = fluwx.openWeChatApp();

    print("=========结果<<<$result>>>====");
  }

  void setGift(index) {
    print(state.isGiftMoney);
    setcard(state, index);
    update();
  }

  // add by gyq
  //通过状态获取下标
  int getTabIndex() {
    int index = 0;
    int orderstatus = state.orderStatus;
    print('_getTabIndex===');
    print(orderstatus);
    switch (orderstatus) {
      case 1:
        index = 1;
        break;
      case 2:
        index = 2;
        break;
      case 3:
        index = 3;
        break;
      case 4:
        index = 4;
        break;
      case 17:
        index = 5;
        break;
    }
    return index;
  }

  //通过下标获取状态
  void upgradeStateOrderStatus() {
    switch (state.index) {
      case 0:
        state.orderStatus = 0;
        break;
      case 1:
        state.orderStatus = 1;
        break;
      case 2:
        state.orderStatus = 2;
        break;
      case 3:
        state.orderStatus = 3;
        break;
      case 4:
        state.orderStatus = 4;
        break;
      case 5:
        state.orderStatus = 17;
        break;
    }
  }
}
